package com.yuexinduo.baselibrary.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.yuexinduo.baselibrary.R;
import com.yuexinduo.baselibrary.util.TUtil;


/**
 * @author DeMon
 * @date 2017/12/18
 * @description Activity 基类
 */
public abstract class BaseMvpActivity<T extends InBasePresenter> extends AppCompatActivity implements BaseView {
    protected final String TAG = this.getClass().getSimpleName();
    public Context mContext;
    protected T mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseModel.mContext = this;
        ARouter.getInstance().inject(this);
        mContext = this;
        initParam(getIntent());
        getActivity();
        initLayout();
        //初始化沉浸式
        initImmersionBar();
        initPresenter();
        initCreate();

    }


    /**
     * 初始化沉浸式
     * Init immersion bar.
     */
    protected void initImmersionBar() {
        //设置共同沉浸式样式
        ImmersionBar.with(this).navigationBarColor(R.color.white).init();
    }


    /**
     * 初始化布局
     * 封装成方法的目的：
     * 例如需要实现含有标题栏的BaseMvpActivity可重写此方法
     */
    protected void initLayout() {
        setContentView(bindLayout());
    }

    /**
     * 获取绑定的布局
     */
    protected abstract int bindLayout();

    /**
     * 获取泛型实例化Presenter
     * 绑定View
     */
    private void initPresenter() {
        mPresenter = TUtil.getT(this);
        if (mPresenter != null) {
            mPresenter.setContext(mContext);
            mPresenter.onStart(this);
        }
    }

    /**
     * 上下文
     *
     * @return
     */
    protected abstract Activity getActivity();

    protected void initParam(Intent intent) {

    }


    protected abstract void initCreate();

    /**
     * 解绑View
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }

    }
}
